package com.example.blackbox.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.example.blackbox.Adapter.OtherNewsAdapter;
import com.example.blackbox.Broadcast.MineBroadcast;
import com.example.blackbox.Data.News;
import com.example.blackbox.DataBase.OpDataBase;
import com.example.blackbox.R;

import java.util.ArrayList;

public class CollectionActivity extends Activity {
    ListView ls;
    ArrayList<News> data;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        initUI();
        OpDataBase opDataBase=new OpDataBase(CollectionActivity.this);
        data = opDataBase.findAllData();
        OtherNewsAdapter adapter=new OtherNewsAdapter(data);
        ls.setAdapter(adapter);

        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(CollectionActivity.this, PageeActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("news", data.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        IntentFilter filter=new IntentFilter();
        filter.addAction("DELETE_NEWS_DATA_ACTION");
        MineBroadcast myBroadCastReceiver=new MineBroadcast(CollectionActivity.this);
        registerReceiver(myBroadCastReceiver,filter);
    }

    public void initUI(){
        ls=(ListView)findViewById(R.id.collection_ls);
    }
}
